package com.gxc.mq;

import com.gxc.enums.StatusEnum;
import com.gxc.dao.MqLogRepository;
import com.gxc.entity.MqLog;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.transaction.Transactional;

/**
 * @author GongXincheng
 * @since 2019-08-27 15:16
 */
@Component
public class ExceptionProviderMq {

    @Resource
    private AmqpTemplate amqpTemplate;

    @Resource
    private MqLogRepository mqLogRepository;

    /** 发送消息. */
//  @Transactional(rollbackOn = Exception.class)
    public void saveMessage() throws InterruptedException {
        MqLog mqLog = new MqLog();
        mqLog.setParams("{name: hello}");
        mqLog.setStatus(StatusEnum.STATUS_NO.getCode());
        MqLog log = saveMqLog(mqLog);

        amqpTemplate.convertAndSend("exception", log.getId());
        Thread.sleep(1000L);
    }

    @Transactional(rollbackOn = Exception.class)
    public MqLog saveMqLog(MqLog mqLog) {
        return mqLogRepository.save(mqLog);
    }

}
